/**
 * Formats a channel for emitter.io protocol.
 *
 * @private
 * @param {string} key The key to use.
 * @param {string} channel The channel name.
 * @param {...Option[]} options The list of options to apply.
 * @returns
 */
export default { 
    FormatEmitterTopic (key, channel, options) {
        // Prefix with the key
        var formatted = this._endsWith(key, "/")
            ? key + channel
            : key + "/" + channel;
        // Add trailing slash
        if (!this._endsWith(formatted, "/"))
            formatted += "/";
        // Add options
        if (options != null && options.length > 0) {
            formatted += "?";
            for (var i = 0; i < options.length; ++i) {
                formatted += options[i].key + "=" + options[i].value;
                if (i + 1 < options.length)
                    formatted += "&";
            }
        }
        // We're done compiling the channel name
        return formatted;
    },
    DecodeEmitterTopic( topic ) { 
        var off = topic.indexOf( '/')
        var key = topic.subString(0,  off )
        var channel = topic.subString( off+ 1 )
        return { key, channel }
    }

}
